/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Handlers;

import apiAccess.*;
import java.io.IOException;
import java.util.List;
import org.xml.sax.SAXException;

/**
 *
 * @author Alan
 */
public class CategoryHandler {
    List<Category> categoryList;
    int from;
    int to;
    int cant;
    int page;
    public CategoryHandler(int cant, int languageId) throws SAXException, IOException, APIEXception{
        this.cant=cant;
        this.from=0-cant;
        this.to=0;
        page=0;
        categoryList=APIAccess.getCategoryList(languageId);
    }

 public List<Category> next(){
       if(from+cant>=categoryList.size()){
           return null;
       }
       from+=cant;
       page++;
       if(to+cant>=categoryList.size()){
            to=categoryList.size();
       }else{
           to+=cant;
       }
       List<Category> ret= categoryList.subList(from, to);
       return ret;
   }

   public List<Category> back(){
       if(from==0){
           return null;
       }
       page--;
       to-=to-from;
       from-=cant;
       List<Category> ret= categoryList.subList(from, to);
       return ret;
   }

   public boolean activoNext(){
       if(from+cant>=categoryList.size()){
           return false;
       }
       return true;

   }

    public boolean activoBack(){
       if(from==0){
           return false;
       }
       return true;

   }

    public int cantCategories() {
        if(hayCat())
            return categoryList.size();
        return 0;
    }

    public boolean hayCat(){
        return categoryList!=null;
    }

    public int pages() {
        int aux=categoryList.size()%this.cant;
        int resp=(int)(categoryList.size()/this.cant);
        if(aux!=0){
            resp++;
        }
        return resp;
    }

    public String pagesFoot(){
        return page+"/"+pages();
    }
}
